﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.Biz;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;
using Sector7.Medical.Consts;
using System.ComponentModel;
using Sector7.Medical.Enums;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// DrugItemControl.xaml 的交互逻辑
    /// </summary>
    public partial class DrugItemControl : UserControl
    {
        #region Accessor
        public static readonly DependencyProperty TemplateTypeProperty =
            DependencyProperty.Register("TemplateType", typeof(DrugTemplateType), typeof(DrugItemControl), new PropertyMetadata(DrugTemplateType.PurchasesBill));

        [Description("获取或设置模板类型")]
        [Category("其他")]
        public DrugTemplateType TemplateType
        {
            get { return (DrugTemplateType)this.GetValue(TemplateTypeProperty); }
            set { this.SetValue(TemplateTypeProperty, value); }
        }

        /// <summary>
        /// 点击回车事件
        /// </summary>
        public event EventHandler ClickOk = null;

        /// <summary>
        /// 点击删除事件
        /// </summary>
        public event EventHandler ClickDelete = null;

        /// <summary>
        /// 药品项改变事件
        /// </summary>
        public event EventHandler ItemUpdated = null;
        #endregion

        #region Public functions
        public M7_PurchasesBillItem GetPurchasesBillItem(long billId)
        {
            M7_PurchasesBillItem item = null;
            try
            {
                item = new M7_PurchasesBillItem();
                var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
                item.BillId = billId;
                item.DrugId = selectedDrug.DrugId;
                item.ItemId = m_uniqueId;

                decimal spec;
                M7_Unit packUnit, stockUnit;
                stockSpec.GetSpec(out spec, out packUnit, out stockUnit);
                //item.StockSpec = spec;
                //item.PackUnitId = packUnit.UnitId;
                //item.StockUnitId = stockUnit.UnitId;
                item.Number = Convert.ToDecimal(txbNumber.Text);
                item.PurchasesPrice = Convert.ToDecimal(txbPurchasesPrice.Text);
                item.SalesPrice = Convert.ToDecimal(txbSalesPrice.Text);
                item.BatchNumber = txbBatchNumber.Text;
                item.LotNumber = Convert.ToInt32(txbLotNumber.Text);
                item.ProducedDate = (DateTime)dtpProducedDate.SelectedDate;
                item.AvailableDate = (DateTime)dtpAvailibleDate.SelectedDate;
            }
            catch (Exception)
            {
                item = null;
            }

            return item;
        }

        public void SetAdjustRange(decimal rangeOfPurchases, decimal rangeOfSales)
        {
            m_rangeOfPurchases = rangeOfPurchases;
            m_rangeOfSales = rangeOfSales;

            UpdatePrice(false);
        }
        #endregion

        #region Private functions
        private void UpdatePrice(bool ignoreExistPrice)
        {
            var selectedDrug = cbbDrugList2.SelectedItem as M7_DrugPrice;
            if (selectedDrug != null)
            {
                if (ignoreExistPrice || (!ignoreExistPrice && string.IsNullOrWhiteSpace(txbNewPurchasesPrice2.Text)))
                {
                    txbNewPurchasesPrice2.Text = (selectedDrug.PurchasesPrice * (1 + m_rangeOfPurchases)).ToString();
                }
                if (ignoreExistPrice || (!ignoreExistPrice && string.IsNullOrWhiteSpace(txbNewSalesPrice2.Text)))
                {
                    txbNewSalesPrice2.Text = (selectedDrug.SalesPrice * (1 + m_rangeOfSales)).ToString();
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.MaxHeight = 30;

                m_uniqueId = DateTime.Now.Ticks;
                stpMain.Children.Clear();
                switch (TemplateType)
                {
                    case DrugTemplateType.PurchasesBill:
                        cbbDrugList.TemplateType = TemplateType;
                        stpMain.Children.Add(grdPurchasesBill);
                        if (dtpProducedDate.SelectedDate == null)
                        {
                            dtpProducedDate.SelectedDate = DateTime.Now;
                        }
                        if (dtpAvailibleDate.SelectedDate == null)
                        {
                            dtpAvailibleDate.SelectedDate = DateTime.Now;
                        }
                        break;
                    case DrugTemplateType.AdjustRecord:
                        cbbDrugList2.TemplateType = TemplateType;
                        stpMain.Children.Add(grdAdjustRecord);
                        break;
                    default:
                        break;
                }

                var packUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.PACK_UNIT);
                var stockUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.STOCK_UNIT);
                stockSpec.InitializeUnitList(packUnitList, stockUnitList);
            }
            catch (Exception)
            {
            }
        }

        private void OnClickOk(EventArgs e)
        {
            if (ClickOk != null)
            {
                ClickOk(this, e);
            }
        }

        private void OnClickDelte(EventArgs e)
        {
            if (ClickDelete != null)
            {
                ClickDelete(this, e);
            }
        }

        private void OnItemUpdated(EventArgs e)
        {
            if (ItemUpdated != null)
            {
                ItemUpdated(this, e);
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            OnClickOk(new EventArgs());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            OnClickDelte(new EventArgs());
        }

        private void cbbDrugList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
            if (selectedDrug != null)
            {
                lblDrugId.Content = selectedDrug.DrugId.ToString();
                lblProductName.Content = selectedDrug.ProductName;
                //lblSpec.Content = selectedDrug.Spec.ToString();
                //packSpec.SetSpec(selectedDrug.Spec, selectedDrug.UsageUnit, selectedDrug.PackUnit);
            }
            OnItemUpdated(e);
        }

        private void cbbDrugList2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = cbbDrugList2.SelectedItem as M7_DrugPrice;
            if (selectedDrug != null)
            {
                lblDrugId2.Content = selectedDrug.DrugId.ToString();
                //lblProductName2.Content = selectedDrug.Drug.ProductName;
                //lblSpec2.Content = selectedDrug.Drug.Spec.ToString();
                //packSpec2.SetSpec(selectedDrug.Drug.Spec, selectedDrug.Drug.UsageUnit, selectedDrug.Drug.PackUnit);
                lblPurchasesPrice2.Content = selectedDrug.PurchasesPrice.ToString();
                lblSalesPrice2.Content = selectedDrug.SalesPrice.ToString();

                UpdatePrice(true);
            }
            OnItemUpdated(e);
        }

        private void stockSpec_SpecUpdated(object sender, EventArgs e)
        {
            OnItemUpdated(new EventArgs());
        }

        private void txb_TextChanged(object sender, TextChangedEventArgs e)
        {
            OnItemUpdated(new EventArgs());
        }

        private void dtp_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            OnItemUpdated(new EventArgs());
        }
        #endregion

        #region Structure
        public DrugItemControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private long m_uniqueId;
        private decimal m_rangeOfPurchases;
        private decimal m_rangeOfSales;
        #endregion
    }
}
